<template>
  <div class="imgUrl">
    <el-upload
      class="avatar-uploader"
      action="*"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img
        v-if="imageUrl"
        v-imgerror="defaultImg"
        :src="imageUrl"
        class="avatar"
        @click.stop="showBigImage"
      >
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <div v-if="imageUrl" class="close" @click="closeEvent">X</div>
    <el-dialog :visible.sync="isshow" title="图片预览">
      <div style="text-align: center">
        <img class="bigimg" :src="imageUrl" alt="">
      </div>
    </el-dialog>
    <div v-if="showprogress" class="box" />
    <el-progress
      v-if="showprogress"
      class="progress"
      type="circle"
      :percentage="percentage"
    />
  </div>
</template>
<script>
import Cos from 'cos-js-sdk-v5'
const cos = new Cos({
  SecretId: 'AKIDiCSR9akeIfmb9QP8ZjcEZcOu9bmfIfla',
  SecretKey: 'atvgANJxKRHq8AvOaWBZhY8RTEivrXx6'
})
export default {
  name: 'ImgUrl',
  model: {
    prop: 'imageUrl'
  },
  props: {
    imageUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      defaultImg: require('@/assets/common/head.jpg'),
      showprogress: false,
      percentage: 0,
      isshow: false
      // imageUrl: ''
    }
  },

  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      this.showprogress = true
      this.percentage = 0
      console.log(file)
      const ispng = file.type === 'image/png'
      const isLt1M = file.size / 1024 / 1024 < 1

      if (!ispng) {
        this.$message.error('上传头像图片只能是 PNG 格式!')
      }
      if (!isLt1M) {
        this.$message.error('上传头像图片大小不能超过 1MB!')
      }
      return ispng && isLt1M
    },
    httpRequest(res) {
      cos.putObject(
        {
          Bucket: 'first-1308460173' /* 必须 */,
          Region: 'ap-chongqing' /* 存储桶所在地域，必须字段 */,
          Key: res.file.uid + '' /* 必须 */,
          StorageClass: 'STANDARD',
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            console.log(progressData)
            this.percentage = progressData.percent * 100
            if (progressData.percent === 1) {
              setTimeout(() => {
                this.showprogress = false
              }, 1000)
            }
          }
        },
        (err, data) => {
          console.log(err || data)
          console.log(this, 'this')
          this.$emit('input', 'http://' + data.Location)
        }
      )
    },
    showBigImage() {
      this.isshow = true
    },
    closeEvent() {
      this.$emit('input', '')
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.imgUrl {
  position: relative;
  width: 178px;
  &:hover {
    .close {
      display: block;
    }
  }
  .close {
    display: none;
    position: absolute;
    top: 5px;
    right: 10px;
    cursor: pointer;
  }
  .bigimg {
    width: 100%;
  }
  ::v-deep.box {
    width: 178px;
    height: 178px;
    position: absolute;
    top: 0px;
    left: 0px;
    opacity: 0.3;
    background-color: #000;
  }
  ::v-deep.progress {
    position: absolute;
    top: 26px;
    left: 26px;
  }
}
</style>
